﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XmlStudio.DataLayer;
using XmlStudio.Interfaces;
using XmlStudio.Models;
using XmlStudio.Utilities;
using XmlStudio.Enums;
using System.Xml.XPath;

namespace XmlStudio.Tests {
    /// <summary>
    ///This is a test class for BigXmlNodeTest and is intended
    ///to contain all BigXmlNodeTest Unit Tests
    ///</summary>
    [TestClass()]
    public class BigXmlNodeTest {
        #region Fields
        private TestContext testContextInstance;
        #endregion

        #region Properties
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }
        #endregion

        #region Additional test simpleTypes
        // 
        //You can use the following additional simpleTypes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        #region MyBigXmlNode constructor tests
        /// <summary>
        /// A test for MyBigXmlNode Constructor.
        ///</summary>
        [TestMethod()]
        public void BigXmlNodeConstructorTest() {
            int[] address = new int[] { 1, 2, 3 };
            var node = BigXmlNodeInternal.Create(address);
            Assert.AreEqual<int[]>(address, node.Address);

            var node2 = BigXmlNodeInternal.Create(new int[] { 1 });
            Assert.IsNotNull(node2);
            Assert.IsNull(node2.Parent);
            Assert.AreEqual<int>(0, node2.ChildNodes.Count);
        }

        /// <summary>
        /// A test for MyBigXmlNode Constructor.
        /// </summary>
        [TestMethod()]
        public void BigXmlNodeConstructorTest2() {
            var doc = new XmlDocument();
            var xmlNode = doc.CreateNode(XmlNodeType.Element, "test", "");

            int[] address = new int[] { 1, 2, 3 };
            var node = BigXmlNodeInternal.Create(xmlNode, address, address.DeepClone());
            Assert.AreEqual<int[]>(address, node.Address);
            Assert.AreEqual<IXPathNavigable>(xmlNode, node.XmlNode);
        }
        #endregion

        #region GetLeftSibling tests
        [TestMethod()]
        public void GetPreviousSiblingTest01() {
            var model = ModelFactory.CreateXmlModel();
            model.Open(Common.PathShiporder);

            var root = model.GetRootElement();

            model.ExpandNode(root, string.Empty);

            var orderPerson = root.ChildNodes[0];
            var shipTo = root.ChildNodes[1];
            var item1 = root.ChildNodes[2];
            var item2 = root.ChildNodes[3];
            var lest = root.ChildNodes[4];

            var previousSibling1 = root.GetPreviousSibling(shipTo);
            Assert.IsTrue(previousSibling1 == orderPerson);

            var previousSibling2 = root.GetPreviousSibling(orderPerson);
            Assert.IsNull(previousSibling2);

            var previousSibling3 = root.GetPreviousSibling(lest);
            Assert.IsTrue(previousSibling3 == item2);
        }
        #endregion

        #region GetRightSibling tests
        [TestMethod()]
        public void GetNextSiblingTest01() {
            var model = ModelFactory.CreateXmlModel();
            model.Open(Common.PathShiporder);

            var root = model.GetRootElement();

            model.ExpandNode(root, string.Empty);

            var orderPerson = root.ChildNodes[0];
            var shipTo = root.ChildNodes[1];
            var item1 = root.ChildNodes[2];
            var item2 = root.ChildNodes[3];
            var lest = root.ChildNodes[4];

            var nextSibling1 = root.GetNextSibling(orderPerson);
            Assert.IsTrue(nextSibling1 == shipTo);

            var nextSibling2 = root.GetNextSibling(lest);
            Assert.IsNull(nextSibling2);

            var nextSibling3 = root.GetNextSibling(item1);
            Assert.IsTrue(nextSibling3 == item2);
        }
        #endregion

        #region CanCollapse tests
        /// <summary>
        /// CanCollapse test.
        /// </summary>
        [TestMethod()]
        public void CanCollapseTest01() {
            var model = ModelFactory.CreateXmlModel();
            model.Open(Common.PathTest1);

            var root = model.GetRootElement();

            model.ExpandNode(root, string.Empty);

            var categories = root.ChildNodes[0];
            var stock = root.ChildNodes[1];

            model.ExpandNode(categories, string.Empty);
            model.ExpandNode(stock, string.Empty);

            var category1 = categories.ChildNodes[0];
            var category2 = categories.ChildNodes[1];

            var books = stock.ChildNodes[0];
            var cds = stock.ChildNodes[1];

            model.RenameElement(cds, "cdsnew");

            Assert.IsFalse(root.CanCollapse());
            Assert.IsFalse(stock.CanCollapse());
            Assert.IsTrue(categories.CanCollapse());
            Assert.IsTrue(books.CanCollapse());
        }

        /// <summary>
        /// CanCollapse test.
        /// </summary>
        [TestMethod()]
        [ExpectedException(typeof(InvalidOperationException))]
        public void CanCollapseTest02() {
            var model = ModelFactory.CreateXmlModel();
            model.Open(Common.PathTest1);

            var root = model.GetRootElement();

            model.ExpandNode(root, string.Empty);

            var categories = root.ChildNodes[0];
            var stock = root.ChildNodes[1];

            model.ExpandNode(categories, string.Empty);
            model.ExpandNode(stock, string.Empty);

            var category1 = categories.ChildNodes[0];
            var category2 = categories.ChildNodes[1];

            var books = stock.ChildNodes[0];
            var cds = stock.ChildNodes[1];

            model.RenameElement(cds, "cdsnew");

            Assert.IsFalse(root.CanCollapse());
            Assert.IsFalse(stock.CanCollapse());
            Assert.IsTrue(categories.CanCollapse());
            Assert.IsTrue(books.CanCollapse());

            model.CollapseNode(root);
        }

        /// <summary>
        /// CanCollapse test.
        /// </summary>
        [TestMethod()]
        public void CanCollapseTest03() {
            var model = ModelFactory.CreateXmlModel();
            model.Open(Common.PathTest1);

            var root = model.GetRootElement();

            model.ExpandNode(root, string.Empty);

            var categories = root.ChildNodes[0];
            var stock = root.ChildNodes[1];

            model.ExpandNode(categories, string.Empty);
            model.ExpandNode(stock, string.Empty);

            var category1 = categories.ChildNodes[0];
            var category2 = categories.ChildNodes[1];

            var books = stock.ChildNodes[0];
            var cds = stock.ChildNodes[1];

            model.RenameElement(cds, "cdsnew");

            Assert.IsFalse(root.CanCollapse());
            Assert.IsFalse(stock.CanCollapse());
            Assert.IsTrue(categories.CanCollapse());
            Assert.IsTrue(books.CanCollapse());

            model.CollapseNode(categories);
            Assert.IsTrue(categories.NodeState == Enums.ExpandState.Collapsed);
        }
        #endregion

        //#region Modified tests
        ///// <summary>
        ///// Test for Modified property of the <see cref="MyBigXmlNode"/> class.
        ///// Node must be modified after its address changes.
        ///// </summary>
        //[TestMethod()]
        //public void ModifiedAddressTest()
        //{
        //    XmlDocument doc = new XmlDocument();
        //    XmlNode xmlNode = doc.CreateNode(XmlNodeType.Element, "test", "");
        //    MyBigXmlNode param = new MyBigXmlNode(new int[] { 1, 1 }, xmlNode);

        //    Assert.IsFalse(param.Modified);

        //    param.Address = new int[] { 1, 2 };

        //    Assert.IsTrue(param.Modified);
        //}

        ///// <summary>
        ///// Test for Modified property of the <see cref="MyBigXmlNode"/> class.
        ///// Node must be modified after its param changes.
        ///// </summary>
        //[TestMethod()]
        //public void ModifiedParentTest()
        //{
        //    XmlDocument doc = new XmlDocument();
        //    XmlNode xmlNode = doc.CreateNode(XmlNodeType.Element, "test", "");
        //    MyBigXmlNode param = new MyBigXmlNode(new int[] { 1, 1 }, xmlNode);

        //    Assert.IsFalse(param.Modified);

        //    param.Parent = new MyBigXmlNode();

        //    Assert.IsTrue(param.Modified);
        //}

        ///// <summary>
        ///// Test for Modified property of the <see cref="MyBigXmlNode"/> class.
        ///// Node must be modified after its xmlNode changes.
        ///// </summary>
        //[TestMethod()]
        //public void ModifiedXmlNodeTest()
        //{
        //    XmlDocument doc = new XmlDocument();
        //    XmlNode xmlNode = doc.CreateNode(XmlNodeType.Element, "test", "");
        //    MyBigXmlNode param = new MyBigXmlNode(new int[] { 1, 1 }, xmlNode);

        //    Assert.IsFalse(param.Modified);

        //    param.XmlNode = null;

        //    Assert.IsTrue(param.Modified);
        //}
        //#endregion

        #region Save tests
        [TestMethod()]
        public void SaveTest01() {
            var model = ModelFactory.CreateXmlModel() as XmlModel;
            model.Open(Common.PathTest1);

            var root = model.GetRootElement() as BigXmlNodeInternal;

            StringBuilder sb = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter w = XmlWriter.Create(sb, settings);

            root.Save(w, model.Reader, true, null);
            w.Close();

            String xml = sb.ToString();
            Assert.IsTrue(!string.IsNullOrEmpty(xml));
            Assert.AreEqual<string>(
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<root>
  <categories>
    <category>
      <item>
        <description>Item 1</description>
      </item>
      <item>
        <description>Item 2</description>
      </item>
      <item>
        <description>Item 3</description>
      </item>
      <item>
        <description>Item 4</description>
      </item>
      <item>
        <description>Item 5</description>
      </item>
    </category>
    <category>
      <item>
        <description>Item 6</description>
      </item>
    </category>
  </categories>
  <stock>
    <books>
      <book>
        <title>Book 1</title>
      </book>
      <book>
        <title>Book 2</title>
      </book>
    </books>
    <cds>
      <cd>
        <title>CD 1</title>
      </cd>
      <cd>
        <title>CD 2</title>
      </cd>
    </cds>
  </stock>
</root>", xml);
        }

        /// <summary>
        /// Save testing AddAttributeAction.
        /// </summary>
        [TestMethod()]
        public void SaveTest02() {
            var model = ModelFactory.CreateXmlModel() as XmlModel;
            model.Open(Common.PathTest1);

            var root = model.GetRootElement() as BigXmlNodeInternal;
            model.AddAttribute(root, "testAtt", "testValue");

            StringBuilder sb = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter w = XmlWriter.Create(sb, settings);

            root.Save(w, model.Reader, true, null);
            w.Close();

            String xml = sb.ToString();
            Assert.IsTrue(!string.IsNullOrEmpty(xml));
            Assert.AreEqual<string>(
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<root testAtt=""testValue"">
  <categories>
    <category>
      <item>
        <description>Item 1</description>
      </item>
      <item>
        <description>Item 2</description>
      </item>
      <item>
        <description>Item 3</description>
      </item>
      <item>
        <description>Item 4</description>
      </item>
      <item>
        <description>Item 5</description>
      </item>
    </category>
    <category>
      <item>
        <description>Item 6</description>
      </item>
    </category>
  </categories>
  <stock>
    <books>
      <book>
        <title>Book 1</title>
      </book>
      <book>
        <title>Book 2</title>
      </book>
    </books>
    <cds>
      <cd>
        <title>CD 1</title>
      </cd>
      <cd>
        <title>CD 2</title>
      </cd>
    </cds>
  </stock>
</root>", xml);
        }

        /// <summary>
        /// Save testing AddChildNodeAction.
        /// </summary>
        [TestMethod()]
        public void SaveTest03() {
            var model = ModelFactory.CreateXmlModel() as XmlModel;
            model.Open(Common.PathTest1);

            var root = model.GetRootElement() as BigXmlNodeInternal;

            model.ExpandNode(root);

            var doc = model.XmlDocument;
            var element = doc.CreateElement("testElement");

            var node = BigXmlNodeInternal.Create(element);

            model.AddChildNode(root, node, InsertMode.First, null);

            StringBuilder sb = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter w = XmlWriter.Create(sb, settings);

            root.Save(w, model.Reader, true, null);
            w.Close();

            String xml = sb.ToString();
            Assert.IsTrue(!string.IsNullOrEmpty(xml));
            Assert.AreEqual<string>(
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<root>
  <testElement />
  <categories>
    <category>
      <item>
        <description>Item 1</description>
      </item>
      <item>
        <description>Item 2</description>
      </item>
      <item>
        <description>Item 3</description>
      </item>
      <item>
        <description>Item 4</description>
      </item>
      <item>
        <description>Item 5</description>
      </item>
    </category>
    <category>
      <item>
        <description>Item 6</description>
      </item>
    </category>
  </categories>
  <stock>
    <books>
      <book>
        <title>Book 1</title>
      </book>
      <book>
        <title>Book 2</title>
      </book>
    </books>
    <cds>
      <cd>
        <title>CD 1</title>
      </cd>
      <cd>
        <title>CD 2</title>
      </cd>
    </cds>
  </stock>
</root>", xml);
        }

        /// <summary>
        /// Test for Save with CreateRootElementAction.
        /// </summary>
        [TestMethod()]
        public void SaveTest04() {
            var model = ModelFactory.CreateXmlModel() as XmlModel;
            model.CreateRootElement("rootElement");

            var root = model.GetRootElement() as BigXmlNodeInternal;

            Assert.IsNotNull(root);
            Assert.IsTrue(root.NodeState == Enums.ExpandState.Expanded);
            Assert.IsNull(root.OriginalAddress);
            Assert.IsTrue(root.Address.SequenceEqual(new int[] { 1 }));
            Assert.IsTrue(root.ChildNodes.Count == 0);
            Assert.IsTrue(root.Modified);

            var doc = model.XmlDocument;

            var child1 = doc.CreateElement("child1");
            var child1Node = BigXmlNodeInternal.Create(child1);

            var child2 = doc.CreateElement("child2");
            var child2Node = BigXmlNodeInternal.Create(child2);

            var text = doc.CreateTextNode("test");
            var textNode = BigXmlNodeInternal.Create(text);

            model.AddChildNode(root, child1Node, InsertMode.First, null);
            model.AddChildNode(root, child2Node, InsertMode.After, child1Node);
            model.AddChildNode(child1Node, textNode, InsertMode.First, null);

            Assert.IsTrue(root.ChildNodes.Count == 2);

            Assert.IsTrue(child1Node.ChildNodes.Count == 1);

            Assert.IsTrue(child1Node.NodeState == Enums.ExpandState.Expanded);
            Assert.IsTrue(child2Node.NodeState == Enums.ExpandState.Expanded);

            Assert.IsTrue(child1Node.Modified);
            Assert.IsTrue(child2Node.Modified);
            Assert.IsTrue(textNode.Modified);

            Assert.IsNull(child1Node.OriginalAddress);
            Assert.IsNull(child2Node.OriginalAddress);
            Assert.IsNull(textNode.OriginalAddress);

            Assert.IsNotNull(child1Node.Address);
            Assert.IsNotNull(child2Node.Address);
            Assert.IsNotNull(textNode);

            StringBuilder sb = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter w = XmlWriter.Create(sb, settings);

            root.Save(w, model.Reader, true,null);
            w.Close();

            String xml = sb.ToString();
            Assert.IsTrue(!string.IsNullOrEmpty(xml));
            Assert.AreEqual<string>(
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<rootElement>
  <child1>test</child1>
  <child2 />
</rootElement>", xml);

            Assert.IsTrue(root.Address.SequenceEqual(root.OriginalAddress));
            Assert.IsTrue(child1Node.Address.SequenceEqual(child1Node.OriginalAddress));
            Assert.IsTrue(child2Node.Address.SequenceEqual(child2Node.OriginalAddress));

            Assert.IsFalse(root.Modified);
            Assert.IsFalse(child1Node.Modified);
            Assert.IsFalse(child2Node.Modified);
            Assert.IsFalse(textNode.Modified);

            // deep cloning test
            int[] array1 = new int[] { 1, 2, 3 };
            int[] array2 = array1.DeepClone<int[]>();

            array2[0] = 9;
            Assert.IsFalse(array1.SequenceEqual(array2));
        }

        /// <summary>
        /// Test for Save using ChangeNodeOrderAction.
        /// </summary>
        [TestMethod()]
        public void SaveTest05() {
            var model = ModelFactory.CreateXmlModel() as XmlModel;
            model.Open(Common.PathBible1);

            var root = model.GetRootElement() as BigXmlNodeInternal;

            Assert.IsNotNull(root);
            Assert.IsTrue(root.NodeState == Enums.ExpandState.Collapsed);
            Assert.IsNotNull(root.OriginalAddress);
            Assert.IsTrue(root.Address.SequenceEqual(new int[] { 1 }));
            Assert.AreEqual<int>(2, root.ChildNodes.Count);
            Assert.IsFalse(root.Modified);

            model.ExpandNode(root);

            var oldTestament = root.ChildNodes[0];
            var newTestament = root.ChildNodes[1];

            model.ChangeNodeOrder(newTestament, null);

            Assert.IsTrue(root.ChildNodes[0] == newTestament);
            Assert.IsTrue(root.ChildNodes[1] == oldTestament);
            Assert.IsFalse(oldTestament.Modified);
            Assert.IsFalse(newTestament.Modified);
            Assert.IsTrue(root.Modified);

            FileStream fs = new FileStream("SaveTest05Output.xml", FileMode.Create);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter w = XmlWriter.Create(fs, settings);

            root.Save(w, model.Reader, true, null);
            w.Close();
            fs.Close();

            bool areEqual = "SaveTest05Output.xml".FileCompare("SaveTest05OutputCorrect.xml");
            Assert.IsTrue(areEqual);
        }

        [TestMethod()]
        public void SaveTest06() {
            var model = ModelFactory.CreateXmlModel() as XmlModel;
            model.Open(Common.PathTest1);

            var root = model.GetRootElement() as BigXmlNodeInternal;

            model.ExpandNode(root, string.Empty);

            var categories = root.ChildNodes[0];
            var stock = root.ChildNodes[1];

            model.ExpandNode(categories, string.Empty);

            var category1 = categories.ChildNodes[0];
            var category2 = categories.ChildNodes[1];

            model.ExpandNode(category1, string.Empty);

            var item1 = category1.ChildNodes[0];
            var item2 = category1.ChildNodes[1];
            var item3 = category1.ChildNodes[2];
            var item4 = category1.ChildNodes[3];
            var item5 = category1.ChildNodes[4];

            model.ExpandNode(item1, string.Empty);

            var description = item1.ChildNodes[0];

            model.ExpandNode(description, string.Empty);

            var descriptionText = description.ChildNodes[0] as BigXmlNodeInternal;

            Assert.IsNotNull(descriptionText);

            Assert.AreEqual<string>("Item 1", descriptionText.XmlNode.CreateNavigator().Value);

            model.ModifyTextNode(descriptionText, "New Item 1");
            Assert.IsTrue(descriptionText.Modified);

            FileStream fs = new FileStream("SaveTest06Output.xml", FileMode.Create);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter w = XmlWriter.Create(fs, settings);

            root.Save(w, model.Reader, true, null);
            w.Close();
            fs.Close();

            bool areEqual = "SaveTest06Output.xml".FileCompare("SaveTest06OutputCorrect.xml");
            Assert.IsTrue(areEqual);
        }

        /// <summary>
        /// Test for Save testing RemoveAttribute.
        /// </summary>
        [TestMethod()]
        public void SaveTest07() {
            var model = ModelFactory.CreateXmlModel() as XmlModel;
            model.Open(Common.PathTest5);

            var root = model.GetRootElement() as BigXmlNodeInternal;

            model.ExpandNode(root);

            var head = root.ChildNodes[0];
            var body = root.ChildNodes[1];

            model.ExpandNode(body);
            var bookReview = body.ChildNodes[0];
            model.ExpandNode(bookReview);
            var title = bookReview.ChildNodes[0] as BigXmlNodeInternal;
            model.RemoveAttribute(title, "h", "style", "http://www.w3.org/HTML/1998/html4");

            Assert.IsTrue(((XmlNode)title.XmlNode).Attributes.Count == 0);

            FileStream fs = new FileStream("SaveTest07Output.xml", FileMode.Create);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter w = XmlWriter.Create(fs, settings);

            root.Save(w, model.Reader, true, null);
            w.Close();
            fs.Close();

            bool areEqual = "SaveTest07Output.xml".FileCompare("SaveTest07OutputCorrect.xml");
            Assert.IsTrue(areEqual);
        }

        /// <summary>
        /// Test for Save testing RemoveNode with the whole subtree.
        /// </summary>
        [TestMethod()]
        public void SaveTest08() {
            var model = ModelFactory.CreateXmlModel() as XmlModel;
            model.Open(Common.PathTest1);

            var root = model.GetRootElement() as BigXmlNodeInternal;
            model.ExpandNode(root);

            var categories = root.ChildNodes[0];
            var stock = root.ChildNodes[1];

            model.RemoveNode(stock, true);

            Assert.IsTrue(root.Modified);

            FileStream fs = new FileStream("SaveTest08Output.xml", FileMode.Create);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter w = XmlWriter.Create(fs, settings);

            root.Save(w, model.Reader, true, null);
            w.Close();
            fs.Close();

            bool areEqual = "SaveTest08Output.xml".FileCompare("SaveTest08OutputCorrect.xml");
            Assert.IsTrue(areEqual);
        }

        /// <summary>
        /// Test for Save testing RemoveNode with the whole subtree.
        /// </summary>
        [TestMethod()]
        public void SaveTest09() {
            var model = ModelFactory.CreateXmlModel() as XmlModel;
            model.Open(Common.PathTest1);

            var root = model.GetRootElement() as BigXmlNodeInternal;
            model.ExpandNode(root);

            var categories = root.ChildNodes[0];
            var stock = root.ChildNodes[1];

            model.ExpandNode(stock);

            model.RemoveNode(stock, false);

            Assert.IsTrue(root.Modified);

            FileStream fs = new FileStream("SaveTest09Output.xml", FileMode.Create);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter w = XmlWriter.Create(fs, settings);

            root.Save(w, model.Reader, true, null);
            w.Close();
            fs.Close();

            bool areEqual = "SaveTest09Output.xml".FileCompare("SaveTest09OutputCorrect.xml");
            Assert.IsTrue(areEqual);
        }

        /// <summary>
        /// Test for Save() with RenameElement.
        /// </summary>
        [TestMethod()]
        public void SaveTest10() {
            var model = ModelFactory.CreateXmlModel() as XmlModel;
            model.Open(Common.PathTest1);

            var root = model.GetRootElement() as BigXmlNodeInternal;
            model.ExpandNode(root);

            var categories = root.ChildNodes[0];
            var stock = root.ChildNodes[1];

            model.ExpandNode(stock);

            model.RenameElement(stock, "newStock", "http://www.xmlstudio.cz/", "rb");

            Assert.IsTrue(stock.Modified);

            FileStream fs = new FileStream("SaveTest10Output.xml", FileMode.Create);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter w = XmlWriter.Create(fs, settings);

            root.Save(w, model.Reader, true, null);
            w.Close();
            fs.Close();

            bool areEqual = "SaveTest10Output.xml".FileCompare("SaveTest10OutputCorrect.xml");
            Assert.IsTrue(areEqual);

            Assert.IsFalse(root.Modified);
        }

        [TestMethod]
        public void SaveTest11() {
            var model = ModelFactory.CreateXmlModel();
            model.Open(Common.PathTsmt);

            const string savePath = "SaveTest11Output.xml";
            const string savePathCorrect = "SaveTest11OutputCorrect.xml";
            model.Save(savePath, false);

            var areEqual = savePath.XmlCompare(savePathCorrect);
            Assert.IsTrue(areEqual);
        }

        // then for ModifyTextNodeAction, RemoveAttributeAction, RemoveNodeAction, RenameElementAction
        // then load, modify, save, modify, save and check that everything is correct
        // the same with some undo steps
        #endregion

        #region GetAttributes tests
        /// <summary>
        /// Test for GetAttributes() method.
        /// </summary>
        public void GetAttributesTest01() {
            var model = ModelFactory.CreateXmlModel();
            model.Open(Common.PathBible1);

            var root = model.GetRootElement();
            model.ExpandNode(root);

            var oldTestament = root.ChildNodes[0];

            var attributes = oldTestament.Attributes;

            Assert.IsNotNull(attributes);
            Assert.AreEqual<int>(1, attributes.Count());
            Assert.AreEqual<string>("value", attributes["value"].LocalName);
            Assert.AreEqual<string>("OldTestament", attributes["value"].Value);
        }
        #endregion
    }
}